package com.link.base.base.project.service;

import com.link.base.base.product.model.PartsCost;
import com.link.base.base.product.service.PartsCostService;
import com.link.base.base.project.dao.mybatis.mapper.AcceptancePartsMapper;
import com.link.base.base.project.model.AcceptanceList;
import com.link.base.base.project.model.AcceptanceParts;
import com.link.base.core.basic.util.BigDecimalUtils;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 项目承接单零件信息表
 *
 * @author 王昊
 * @date 2020-10-15 13:56:25
 */
@Service
public class AcceptancePartsServiceImpl extends BasicServiceImpl<AcceptanceParts> implements AcceptancePartsService {

    @Resource
    private AcceptancePartsMapper    acceptancePartsMapper;

    @Resource
    private AcceptanceListService acceptanceListService;

    @Resource
    private PartsCostService partsCostService;

    @Override
    public BasicMapper<AcceptanceParts> getBasicMapper() {
        return acceptancePartsMapper;
    }

    /**
     * 产品行保存前计算头的销售额
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforUpsert(AcceptanceParts entity) throws Exception {
        //计算承接单头
        //1.项目毛利 =（均车报价之和-均车成本价之和）/均车报价之和
            //1.1均车报价之和等于该承接单所有产品行零件的均车价之和  根据承接单ID查询对应承接单所有产品行
        AcceptanceParts querParts = new AcceptanceParts();
        querParts.setAcceptanceListId(entity.getAcceptanceListId());
            //查询本承接单的所有产品行
        querParts.setId(entity.getId());
        querParts.setAttr4("notExists");
        List<AcceptanceParts> acceptanceParts = acceptancePartsMapper.queryAll(querParts);
        acceptanceParts.add(entity);
        BigDecimal sumAcctPrice = BigDecimal.ZERO;
        BigDecimal sumAvgVehicleCost = BigDecimal.ZERO;
        if (!acceptanceParts.isEmpty() && acceptanceParts != null) {
            PartsCost queryCost = new PartsCost();
            for (AcceptanceParts acceptancePart : acceptanceParts) {
                //加和所有均车报价
                BigDecimal acctPrice = BigDecimalUtils.changeNullToZero(acceptancePart.getAcctPriceCarPrice());
                sumAcctPrice = sumAcctPrice.add(acctPrice);
                //根据每个产品行的零件成本去零件成本表 查询对应零件的车本均车价
                /*if (acceptancePart.getCRMPartsCostId() == null) {
                    throw new ServiceException("IYUPROJECT-010");
                }
                queryCost.setId(acceptancePart.getCRMPartsCostId());
                PartsCost partsCost = partsCostService.queryById(queryCost);*/
                //1.2均车成本价之和等于该承接单所有产品行零件的成本均车价之和 零件对应成本表成本均车价

                BigDecimal avgVehicleCost = BigDecimal.ZERO; // 对象变更 BigDecimalUtils.changeNullToZero(partsCost.getAvgVehicleCost());
                sumAvgVehicleCost = sumAvgVehicleCost.add(avgVehicleCost);
                // throw new RuntimeException("对象变更，请联系管理员!");
            }
        }

        Long acceptanceId = entity.getAcceptanceListId();
        AcceptanceList acceptanceList = acceptanceListService.queryById(acceptanceId);
        if (acceptanceList != null) {
            //项目毛利
            // acceptanceList.setProjectGross(sumAcctPrice.subtract(sumAvgVehicleCost).divide(sumAcctPrice,4, BigDecimal.ROUND_HALF_UP));
            //年度销售额 = 承接单产品行所有零件的均车价相加之和 * 平均年产量
            BigDecimal avgAnnualOutput = BigDecimalUtils.changeNullToZero(acceptanceList.getAvgAnnualOutput());
            acceptanceList.setAnnualSales(sumAcctPrice.multiply(avgAnnualOutput));
            acceptanceListService.update(acceptanceList);
        }
    }
}
